package praktikumid.w09.p1;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Button panel.
 * @author Ago
 *
 */
public class ButtonPanel extends JPanel {
	
	/**
	 * Reference to the MyFrame. This simplifies
	 * calling frame and DrawPanel.
	 */
	private MyFrame frame;

	/**
	 * Inner class
	 * @author Ago
	 *
	 */
	class DrawRectangleListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println(frame);
			frame.getDrawPanel().drawRectangle(200, 200, 60, 90);
		}
	}
	
	/**
	 * Constructor. We have a parameter for MyFrame,
	 * which is a shortcut (reference) to the frame instance.
	 * That way we can access frame quickly.
	 * @param frame MyFrame instance.
	 */
	public ButtonPanel(MyFrame frame) {
		this.frame = frame;
		setBackground(Color.GRAY);
		
		// let's make buttons
		JButton drawOval = new JButton("draw oval");
		JButton drawRectangle = new JButton("draw rect");
		JButton drawLine = new JButton("draw line");
		
		// let's add listeners, which deal with click
		
		// separate class
		drawOval.addActionListener(new DrawOvalListener());
		// inner class
		drawRectangle.addActionListener(new DrawRectangleListener());
		// anonymous class
		// (declaration and instantiation at the same time)
		drawLine.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// if you don't want to change argument name
				// this refers to current object 
				// (type of ActionListener).
				// frame refers to ButtonPanel constructor's
				// argument.
				// ButtonPanel.this refers to the current
				// ButtonPanel instance.
				ButtonPanel.this.frame.getDrawPanel().drawLine(400, 400, 600, 600);
			}
		});
		
		// let's add buttons to the panel
		add(drawOval);
		add(drawRectangle);
		add(drawLine);
		
	}
}
